package cn.chenxing.module.file.controller;

import cn.chenxing.domain.dto.TrackLine;
import cn.chenxing.domain.dto.TrackPoint;
import cn.chenxing.utils.TableUtil;
import cn.chenxing.domain.threadlocal.ThreadData;
import cn.chenxing.domain.threadlocal.ThreadLocalInfo;
import cn.chenxing.domain.util.DialogBuilderUtil;
import cn.chenxing.module.file.service.FileService;
import cn.chenxing.module.file.view.FileStructAnalysisView;
import com.jfoenix.controls.JFXButton;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;


/**
 * @Description 本地数据控制层
 * @Author maogen.ymg
 * @Date 2020/3/20 0:04
 */
@Lazy
@FXMLController
@Slf4j
public class LocalDataController implements Initializable {

    @FXML
    private AnchorPane anchorPane;

    @FXML
    private Label tipInfo;

    @FXML
    private JFXButton preprocess;

    @FXML
    private JFXButton dataInfo;

    @FXML
    private BorderPane borderPane;

    @Autowired
    private FileService fileService;

    @Autowired
    private FileStructAnalysisView fileStructAnalysisView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        anchorPane.setVisible(false);
        ThreadLocalInfo.get().setLocalDataPane(anchorPane);

        if(ThreadData.get().getTrackTableList() != null) {
            TableUtil.importTableView(borderPane, null, true);
        }
    }

    @FXML
    private void backHomePage() {
        ThreadLocalInfo.get().getOnlineDataPane().setVisible(false);
        ThreadLocalInfo.get().getLocalDataPane().setVisible(false);
        ThreadLocalInfo.get().getFilePane().setVisible(true);
    }

    @FXML
    private void tableImport() {

        fileService.newStageByView(fileStructAnalysisView);
        tipInfo.setText("已选择文件，请进行预处理！");

    }

    @FXML
    private void dataPreprocess() {
        // 原始数据
        // write(ThreadData.get().getTrackData().getTrackLineList(), "aa.txt");
        // 轨迹抽稀
        fileService.dpCompression();
        // write(ThreadData.get().getTrackData().getTrackLineList(), "bb.txt");
        // 卡尔曼滤波
//        if(ThreadData.get().isHasSpeed()) {
//            fileService.kalManFilter();
//            //write(ThreadData.get().getTrackData().getTrackLineList(), "cc.txt");
//        }
        tipInfo.setText("数据完成预处理，可以进行管理");
        DialogBuilderUtil.showPositiveAlert(preprocess,"提示", "数据完成预处理");

        // 表格导入
        TableUtil.importTableView(borderPane, null, true);
    }

    @FXML
    private void dataInfo() {
        fileService.showDataInfo(dataInfo);
    }

    @FXML
    private void exportTable() {
        fileService.exportTable(dataInfo);
    }

    @FXML
    private void uploadServer() {
        fileService.uploadServer(dataInfo);
    }

    private void write(List<TrackLine> trackLinesList, String fileName) {

        try(BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))){

            for (TrackLine trackLine : trackLinesList) {
                List<TrackPoint> coordinate = trackLine.getTrackPointList();

                for (TrackPoint s : coordinate) {
                    String ss = s.getId() + ", " + s.getLongitude() + ", " + s.getLatitude()
                            + ", " + s.getTimeStamp() + ", " + s.getSpeed() + ", " + trackLine.getId();
                    bw.write(ss);
                    bw.newLine();
                    bw.flush();
                }
            }
        }catch (Exception e){
            log.error("输出失败");
        }
    }
}
